//
//  SettingsNetWorkConfigView.m
//  wAudioShare
//
//  Created by Anson on 15-1-24.
//  Copyright (c) 2015年 赵帅. All rights reserved.
//

#import "SettingsNetWorkConfigView.h"



@interface SettingsNetWorkConfigView () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIActionSheetDelegate>

@property (strong, nonatomic) UITableView *listView;
@property (strong, nonatomic) NSMutableArray *dataList;

@end

@implementation SettingsNetWorkConfigView

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)aDataSrc; {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setDataSource:aDataSrc];
        _listView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _listView.dataSource = self;
        _listView.delegate = self;
        _listView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self addSubview:_listView];
    }
    return self;
}

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataList = [NSMutableArray arrayWithCapacity:dataSource.count];
    for (id item in dataSource) {
        if (![item isKindOfClass:NSDictionary.class]) continue;
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:item];
        [_dataList addObject:dict];
    }
    
    [_listView reloadData];
    
}
#pragma mark - TableView Delegate
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//
//UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
//titleLabel.text = @"选择无线网络";
//return titleLabel;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   
    NSString *title = self.dataList[indexPath.row][TEXT_LABEL_KEY];
    UIFont *font = [UIFont systemFontOfSize:18.0];
    cell.textLabel.font = font;
    CGSize size = [title sizeWithFont:font];
    cell.textLabel.text = title;
    
    CGSize sizeTableView = tableView.frame.size;
    UITextField *tf = [self newTextFieldWithSize:CGSizeMake((sizeTableView.width - size.width - 50), 40) txtVal:self.dataList[indexPath.row][TEXT_VALUE_KEY]];
    tf.tag = indexPath.row;
    cell.accessoryView = tf;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, 50);
    UIView *container = [[UIView alloc] initWithFrame:rect];
    //container.backgroundColor = [UIColor yellowColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    btn.bounds = CGRectMake(0, 0, 200, 40);
    btn.center = CGPointMake(rect.size.width/2, rect.size.height/2);
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 0.7;
    [btn addTarget:self action:@selector(btnSaveClicked) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:btn];
    return container;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITextField *tf = (UITextField *)cell.accessoryView;
    [tf becomeFirstResponder];
}

#pragma mark - UITextField
- (UITextField *)newTextFieldWithSize:(CGSize)size txtVal:(NSString *)txt  {
    
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UITextField *tf = [[UITextField alloc] initWithFrame:rect];
    tf.delegate = self;
    tf.text = txt;
    return tf;    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField.tag == 0) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"动态IP(DHCP)" destructiveButtonTitle:@"静态IP" otherButtonTitles:nil];
        [actionSheet showInView:_listView];
        return NO;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.dataList[textField.tag][TEXT_VALUE_KEY] = textField.text;
    
}

#pragma mark - UIActionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        self.dataList[0][TEXT_VALUE_KEY] = @"静态IP";
    }else
        self.dataList[0][TEXT_VALUE_KEY] = @"动态IP(DHCP)";
    
    UITableViewCell *cell = [_listView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UITextField *tf = (UITextField *)cell.accessoryView;
    tf.text = self.dataList[0][TEXT_VALUE_KEY];
}

#pragma mark - UIButton Save Clicked
- (void)btnSaveClicked {
    [self endEditing:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(settingsNetWorkConfigView:didSaveDataSrc:)]) {
        
        NSString *val = self.dataList[0][TEXT_VALUE_KEY];
        NSString *dhcpVal = ([val.lowercaseString rangeOfString:@"dhcp"].location != NSNotFound ? @"true":@"false");
        
        NSDictionary *data = @{@"dhcp":dhcpVal, @"addr":[self.dataList[1][TEXT_VALUE_KEY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]], @"mask":[self.dataList[2][TEXT_VALUE_KEY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]], @"gw":[self.dataList[3][TEXT_VALUE_KEY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]};
        
        [self.delegate performSelector:@selector(settingsNetWorkConfigView:didSaveDataSrc:) withObject:self withObject:data];
    }
}


@end
